define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/stockitems/index' + location.search,
                    add_url: 'warehouse/stockitems/add',
                    edit_url: 'warehouse/stockitems/edit',
                    del_url: 'warehouse/stockitems/del',
                    multi_url: 'warehouse/stockitems/multi',
                    table: 'stockitems',
                },
                queryParams: function (params) { //自定义搜索条件
                    params.ids = Fast.api.query('ids');
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('查看日志'),
                                    title: __('库存日志'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-list',
                                    url: 'warehouse/stock/details'
                                }
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                        }},
                        {field: 'id', title: __('Serial number')},
                        {field: 'product_bn', title: __('Product_bn')},
                        {field: 'batch', title: __('Batch')},
                        {field: 'num', title: __('Num')},
                        {field: 'warehouse.name', title: __('Warehouse.name')},
                        {field: 'location.name', title: __('Location.name')},
                        {field: 'products.name', title: __('Products.name')},
                        {field: 'products.categorys_id', title: __('Products.categorys_id')},
                        {field: 'products.unit', title: __('Products.unit')},
                        {field: 'products.access', title: __('Products.Access'), searchList: {"self":__('Self'),"purchase":__('Purchase'),"outsourcing":__('Outsourcing'),"virtual":__('Virtual'),"custorm":__('Custorm')}, formatter: Table.api.formatter.normal},
                        {field: 'products.status', title: __('Products.Status'), searchList: {"0":__('Products.Status 0'),"1":__('Products.Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ],
                showToggle: false,
                showColumns: true,
                visible: false,
                showExport: false,
                searchFormVisible: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});